package org.devocean.groupbuying.web.backend;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.SerialKey;
import org.devocean.groupbuying.domain.Voucher;
import org.devocean.groupbuying.service.VoucherService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/gb-cpanel/vouchers" })
public class VoucherController {
	
	@Autowired
	private VoucherService  voucherService;
	
	@RequestMapping(value = "/voucher", method = RequestMethod.POST)
	public  @ResponseBody Map<String, ? extends Object> saveVoucher(@RequestBody Voucher voucher) throws Exception{
		try {	
				voucherService.addOrUpdateVoucher(voucher);
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				data.put("voucherID", 1);
				return RenderMapUtils.getMap(data);
		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error saving voutcher into database.");
		}
	
	}
	
	@RequestMapping(value = "/voucher/{discountID}", method = RequestMethod.GET)
	public  @ResponseBody Map<String, ? extends Object> editVoucher(@PathVariable("discountID") Integer discountID) throws Exception{
		try {	
				Voucher voucher = voucherService.getVoucherByDiscountID(discountID);
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				if(voucher != null){		
					SimpleDateFormat simpleFormat = new SimpleDateFormat("MM/dd/yyyy");
					data.put("voucherID", voucher.getVoucherID());
					data.put("expireDate", simpleFormat.format(voucher.getExpireDate()));
					data.put("voucherLocation", voucher.getVoucherLocation());
										
					List<Map<String, Object>> serialsList = new ArrayList<Map<String, Object>>();
					for(SerialKey serialKey : voucher.getSerialKeys()){
						Map<String, Object> serials = new ConcurrentHashMap<String, Object>();
						serials.put("serialKeyID", serialKey.getSerialKeyID());
						serials.put("serial", serialKey.getSerial());
						serials.put("valid", serialKey.getValid());
						serialsList.add(serials);
					}
					data.put("serials", serialsList);
				}
				return RenderMapUtils.getMap(data);
		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error retrieving voutcher into database.");
		}
				
	}
	
	@RequestMapping(value = "/serials/delete", method = RequestMethod.POST)
	public void deleteSerials(String[] requestSelectedSerials){
		if (requestSelectedSerials != null)
			for (String serialID : requestSelectedSerials) {
				voucherService.removeSerial(Integer.parseInt(serialID))	;
			}
			
	}
	
	

}
